package com.zitronella.course.osseventmanager.service;

import com.zitronella.course.osseventmanager.domain.Event;
import com.zitronella.course.osseventmanager.domain.Position;
import com.zitronella.course.osseventmanager.exception.CannotRemovePositionException;


import javax.persistence.EntityTransaction;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.PersistenceException;

import org.springframework.orm.jpa.support.JpaDaoSupport;
import org.springframework.orm.jpa.JpaCallback;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: Robin
 * Date: Feb 23, 2009
 * Time: 1:24:18 AM
 * To change this template use File | Settings | File Templates.
 */
public class PositionServiceImpl extends JpaDaoSupport implements PositionService {

    /**
     * 
     * @param e
     * @param p
     */
    public void addPosition(Event e, Position p){
        p = getJpaTemplate().merge(p);        
        getJpaTemplate().persist(p);
    }
    
    public void removePosition(Event e, Position p){

        if(this.validateNoSignup() == true){
            System.out.println("Here");
            e.removePosition(p);
            //getJpaTemplate().merge(p);
            getJpaTemplate().remove(p);
        }else{
            throw new CannotRemovePositionException(this.toString());
        }
    }

    public boolean validateNoSignup(){

        return true;
    }

    public List<Position> findAllPositions(){
           return (List<Position>) getJpaTemplate().executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                Query q = entityManager.createQuery("from Position p");

                if (q.getResultList().size() >0)return q.getResultList();

                return null;
            }
        });                                
    }

    //A3
    public void updatePosition(Position p){
        p = getJpaTemplate().merge(p);
        getJpaTemplate().persist(p);

    }

    public List<Position> findPosition(String positionName) {

        final String pName = positionName;
        return (List<Position>) getJpaTemplate().executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                Query q = entityManager.createQuery("from Position p where p.name= ? ");
                q.setParameter(1, pName);
                if (q.getResultList().size() >0)return q.getResultList();

                return null;
            }
        });
    }

    // method to find all filled position for an event V4

    public List<Position> findFilledPositionsForEvent(Event e) {
        final String eventName = e.getName();
        return (List<Position>) getJpaTemplate().executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                Query q = entityManager.createQuery("from Position p where p.eventName= ? and p.noOfVolunteerOpenPosition = 0");
                q.setParameter(1, eventName);
                return q.getResultList();
            }
        });
    }

    // method to find future events from a start date V5        
    public List<Position> findOpenPositionsForEvent(Event e) {
        final String eventName = e.getName();
        return (List<Position>) getJpaTemplate().executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                Query q = entityManager.createQuery("from Position p where p.eventName= ? and p.noOfVolunteerOpenPosition > 0");
                q.setParameter(1, eventName);
                return q.getResultList();
            }
        });
    }

    // method to find all position for an event V2
    @SuppressWarnings("unchecked")
    public List<Position> findAllPositionsForEvent(Event event) {
           final String eventName = event.getName();
          return (List<Position>) getJpaTemplate().executeFind(new JpaCallback() {
            public Object doInJpa(EntityManager entityManager) throws PersistenceException {
                Query q = entityManager.createQuery("from Position p where p.eventName =? ");
                q.setParameter(1, eventName);
                return q.getResultList();
            }
        });
    }
}